package com.ys.gulimall.product.app;

import com.ys.common.utils.PageUtils;
import com.ys.common.utils.R;
import com.ys.gulimall.product.entity.ProductAttrValueEntity;
import com.ys.gulimall.product.service.AttrService;
import com.ys.gulimall.product.service.ProductAttrValueService;
import com.ys.gulimall.product.vo.AttrRespVo;
import com.ys.gulimall.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author yeswater
 * @email ruobairuo@gmail.com
 * @date 2021-07-16 17:43:10
 */

@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;
	
	@Resource
	private ProductAttrValueService productAttrValueService;
	
	/**
	 *  获取spu规格
	 */
	@GetMapping("/base/listforspu/{spuId}")
	public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
		
		List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
		
		return R.ok().put("data",entities);
	}
	
	/**
	 * 查询规格参数信息
	 */
	
	// product/attr/sale/list/0?
	// product/attr/base/list/{catelogId}
	@GetMapping("/{attrType}/list/{catelogId}")
	public R baseAttrList(@RequestParam Map<String, Object> params,
	                      @PathVariable("catelogId") Long catelogId,
	                      @PathVariable("attrType")String attrType){
		
		PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
		return R.ok().put("page", page);
	}

    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrId}")
	//@RequiresPermissions("product:attr:info")
	public R info(@PathVariable("attrId") Long attrId){
		
		// AttrEntity attr = attrService.getById(attrId);
		
		AttrRespVo respVo = attrService.getAttrInfo(attrId);
		
		return R.ok().put("attr", respVo);
	}

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
	
	    attrService.updateAttrById(attr);
	
	    return R.ok();
    }
	
	///product/attr/update/{spuId}
	@PostMapping("/update/{spuId}")
	//@RequiresPermissions("product:attr:update")
	public R updateSpuAttr(@PathVariable("spuId") Long spuId,
	                       @RequestBody List<ProductAttrValueEntity> entities){
		
		productAttrValueService.updateSpuAttr(spuId,entities);
		
		return R.ok();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
